using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace EmoticonPanel
{
    public partial class EmoticonsPanel : UserControl
    {
        //an image list object to contain the
        //recieved images for the panel
        private ImageList ilist = new ImageList();
        Size imageSize = new Size(27, 27);

        /// <summary>
        /// an even that send the selceted image
        /// from the panel to the control
        /// </summary>
        public event EventHandler IconClick;
        /// <summary>
        /// the image container of the images
        /// </summary>
        public ImageList Items
        {
            get { return ilist; }
            
        }

        /// <summary>
        /// sets the size of the image in the control
        /// </summary>
        public Size ImageSize
        {
            get { return imageSize; }
            set { imageSize = value; }
        }


        public EmoticonsPanel()
        {
            InitializeComponent();
        }

        /// <summary>
        /// loads an image list to the panel
        /// </summary>
        /// <param name="list">an object containing images</param>
        public void LoadImageList(ImageList list)
        {

            if (list!=null)
            {
                ilist = list;
                //sets the control data to the imageList object
                ilist.TransparentColor = this.BackColor;
                ilist.ImageSize = imageSize;
                //runs throught the images
                for (int i = 0; i < ilist.Images.Count - 1; i++)
                {
                    //creates a picture box
                    PictureBox pb = new PictureBox();
                    //loads the image to the picturebox                
                    pb.Image = ilist.Images[i];
                    pb.Size = ilist.Images[i].Size;
                    //registers for the click event of every picture box
                    pb.Click += new EventHandler(pb_Click);
                    //adds the picture box to a
                    //visualizing container
                    flowLayoutPanel1.Controls.Add(pb);
                }
            }
        }
        
        /// <summary>
        /// shows the panel
        /// </summary>
        public void ShowControl()
        {
            if (ilist!=null || ilist.Images.Count >0)
            {
                this.Show();
                                
            }
            else MessageBox.Show("No images to show");
            
        }
        /// <summary>
        /// the method that runs on the click
        /// event of every picturebox in the container.
        /// send forth the selected image via an event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pb_Click(object sender, EventArgs e)
        {
            if (IconClick != null && sender is PictureBox)
            {
                //converts the recived object to a pictrebox
                //and send it's image with an event
                IconClick(((PictureBox)sender).Image, e);
            }
            //hides the control
            this.Hide();
        }
        /// <summary>
        /// hides the control when it loses focus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EmoticonsPanel_Leave(object sender, EventArgs e)
        {
            this.Hide();
        }     

        
        //two methods for transfering color changes in the control
        private void EmoticonsPanel_ForeColorChanged(object sender, EventArgs e)
        {
            flowLayoutPanel1.ForeColor = this.ForeColor;
        }

        private void EmoticonsPanel_BackColorChanged(object sender, EventArgs e)
        {
            flowLayoutPanel1.BackColor = this.BackColor;
            
        }

        
        


    }
}